package com.sky.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sky.constant.StatusConstant;
import com.sky.context.ThreadLocalUtil;
import com.sky.dto.CategoryDTO;
import com.sky.entity.Category;
import com.sky.exception.BusinessException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private DishMapper dishMapper;

    //添加分类
    @Override
    public void add(CategoryDTO categoryDTO) {
        //1 校验参数
        //2 业务校验
        //3 完成业务
        Category category = BeanUtil.copyProperties(categoryDTO, Category.class);

        //补全数据
        category.setStatus(StatusConstant.ENABLE);
        /*category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        category.setCreateUser(ThreadLocalUtil.getCurrentId());
        category.setUpdateUser(ThreadLocalUtil.getCurrentId());*/
        //添加分类
        try {
            categoryMapper.add(category);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //分页查询
    @Override
    public PageResult page(CategoryDTO categoryDTO) {
        //1. 开启分页
        PageHelper.startPage(categoryDTO.getPage(), categoryDTO.getPageSize());
        //2. 根据条件查询
        List<Category> list = categoryMapper.list(categoryDTO);
        //3. 封装分页结果
        PageInfo<Category> pageInfo = new PageInfo<>(list);
        PageResult result = new PageResult(pageInfo.getTotal(), pageInfo.getList());
        return result;
    }

    //删除分类
    @Override
    public void deleteById(Long id) {
        //1 参数校验（必传）
        //2 业务校验
        //2.1 根据分类id统计关联的菜品数量 如果大于0，说明关联了菜品。不能删除
        Long num = dishMapper.countByCid(id);
        if (num != null && num > 0) {
            throw new BusinessException(500, "当菜分类关联了菜品，不能删除");
        }
        //2.2 根据分类id统计关联的菜品数量 如果大于0 说明关联了菜品不能删除
        num = setmealMapper.countByCid(id);
        if (num != null && num > 0) {
            throw new BusinessException(500, "当菜分类关联了套餐，不能删除");
        }
        //完成业务 调用mapper 根据id删除
        categoryMapper.deleteById(id);
    }

    //更新修改分类
    @Override
    public void update(Category category) {
       /* category.setCreateTime(LocalDateTime.now());
        category.setCreateUser(ThreadLocalUtil.getCurrentId());*/
        categoryMapper.update(category);
    }

    //启用禁用
    @Override
    public void startOrStop(Long id, Integer status) {
        Category category = Category.builder()
                .id(id)
                .status(status)
                .updateUser(ThreadLocalUtil.getCurrentId())
                .updateTime(LocalDateTime.now())
                .build();
        categoryMapper.update(category);
    }
    //菜品分类查询
    @Override
    public List<Category> list(CategoryDTO categoryDTO) {
        return categoryMapper.list(categoryDTO);
    }

}